#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:do_iter3.py
# author: 马国鑫
# datetime:2022/4/7 17:28
# software: PyCharm

"""
this is function  description 
"""


# import module your need

# 迭代器案例：斐波那契数列
# 自定义迭代器类


class Fibnacci(object):
    def __init__(self, num):
        self.num = num  # 要生成的个数
        # 定义变量保存a和b
        self.a = 1
        self.b = 1
        # 记录下标的属性
        self.current_index = 0

    def __iter__(self):
        # 返回自己
        return self

    def __next__(self):
        if self.current_index < self.num:
            data = self.a  # 需要保存a的值，不然下次赋值就变了
            self.a, self.b = self.b, self.a + self.b
            self.current_index += 1
            return data
        else:
            raise StopIteration


if __name__ == '__main__':
    # 创造迭代器对象
    fib_iterator = Fibnacci(5)
    # value = next(fib_iterator)
    # print(value)

    for value in fib_iterator:
        print(value)
